package com.wave.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.wave.common.utils.StringUtils;
import com.wave.common.core.page.TableDataInfo;
import com.wave.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.wave.system.domain.bo.SysAlarmRecordBo;
import com.wave.system.domain.vo.SysAlarmRecordVo;
import com.wave.system.domain.SysAlarmRecord;
import com.wave.system.mapper.SysAlarmRecordMapper;
import com.wave.system.service.ISysAlarmRecordService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 报警记录Service业务层处理
 *
 * @author wave
 * @date 2024-10-12
 */
@RequiredArgsConstructor
@Service
public class SysAlarmRecordServiceImpl implements ISysAlarmRecordService {

    private final SysAlarmRecordMapper baseMapper;

    /**
     * 查询报警记录
     */
    @Override
    public SysAlarmRecordVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询报警记录列表
     */
    @Override
    public TableDataInfo<SysAlarmRecordVo> queryPageList(SysAlarmRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysAlarmRecord> lqw = buildQueryWrapper(bo);
        Page<SysAlarmRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询报警记录列表
     */
    @Override
    public List<SysAlarmRecordVo> queryList(SysAlarmRecordBo bo) {
        LambdaQueryWrapper<SysAlarmRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysAlarmRecord> buildQueryWrapper(SysAlarmRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysAlarmRecord> lqw = Wrappers.lambdaQuery();
        lqw.gt(params.get("beginTime") != null, SysAlarmRecord::getAlarmTime, params.get("beginTime"));
        lqw.lt(params.get("endTime")!=null, SysAlarmRecord::getAlarmType, params.get("endTime"));
        lqw.eq(bo.getAlarmType()!=null, SysAlarmRecord::getAlarmType, bo.getAlarmType());
        lqw.like(StringUtils.isNotBlank(bo.getAlarmContent()), SysAlarmRecord::getAlarmContent, bo.getAlarmContent());
        lqw.orderByDesc(SysAlarmRecord::getAlarmTime);
        return lqw;
    }

    /**
     * 新增报警记录
     */
    @Override
    public Boolean insertByBo(SysAlarmRecordBo bo) {
        SysAlarmRecord add = BeanUtil.toBean(bo, SysAlarmRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改报警记录
     */
    @Override
    public Boolean updateByBo(SysAlarmRecordBo bo) {
        SysAlarmRecord update = BeanUtil.toBean(bo, SysAlarmRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysAlarmRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除报警记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
